/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.model.Buddy;
import sk.yw.azetclient.model.MessageThread;

/**
 *
 * @author  error216
 */
public class ConversationFrame extends javax.swing.JFrame {
    
    private static final long serialVersionUID = 236409244l;
    
    private ConversationManager manager;
    private Buddy user;
    private Buddy buddy;
    private Set<MessageThread> threads = new HashSet<MessageThread>();
    
    /** Creates new form MessageFrame */
    public ConversationFrame(ConversationManager manager,
            Buddy user, Buddy buddy, MessageThread thread) {
        if (manager == null)
            throw new IllegalArgumentException("Null pointer in manager");
        if (user == null)
            throw new IllegalArgumentException("Null pointer in user");
        if (buddy == null)
            throw new IllegalArgumentException("Null pointer in buddy");
        if (thread == null)
            throw new IllegalArgumentException("Null pointer in thread");
        
        this.manager = manager;
        this.user = user;
        this.buddy = buddy;
        
        initComponents();
        
        threads.add(thread);
        createThreadPanel(thread);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        conversationMenu = new javax.swing.JMenu();
        changeConversationName = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(createTitle());
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                ConversationFrame.this.windowClosing(evt);
            }
        });

        conversationMenu.setText(Main.getText("menu.conversation"));

        changeConversationName.setText(Main.getText("menu.conversation.changeConversationName"));
        changeConversationName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeConversationNameActionPerformed(evt);
            }
        });
        conversationMenu.add(changeConversationName);

        jMenuBar1.add(conversationMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void changeConversationNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeConversationNameActionPerformed
        String newName = JOptionPane.showInputDialog(this,
                Main.getText("changeConversationNameDialog.text"),
                Main.getText("changeConversationNameDialog.title"),
                JOptionPane.PLAIN_MESSAGE);
        
        setSelectedTabTitle(newName);
    }//GEN-LAST:event_changeConversationNameActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        manager.removeConversationFrame(buddy);
        dispose();
    }//GEN-LAST:event_windowClosing
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem changeConversationName;
    private javax.swing.JMenu conversationMenu;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
    private String createTitle() {
        return Main.getText("conversationFrame.title")
                .replace("%USERNAME%", buddy.getVisualName());
    }

    public void createThreadPanel(MessageThread thread) {
        threads.add(thread);
        jTabbedPane1.addTab(thread.getName(), 
                new ConversationPanel(this, user, buddy, thread));
    }
    
    public Set<MessageThread> getThreads() {
        return Collections.unmodifiableSet(threads);
    }
    
    public void setSelectedTabTitle(String name) {
        int selectedIndex = jTabbedPane1.getSelectedIndex();
        ConversationPanel selectedPanel =
                (ConversationPanel) jTabbedPane1.getSelectedComponent();
        
        selectedPanel.setMessageThreadName(name);
        jTabbedPane1.setTitleAt(selectedIndex, name);
    }
    
    public boolean messageThreadPanelIsSelected(ConversationPanel panel) {
        return jTabbedPane1.getSelectedComponent().equals(panel);
    }
    
    public void setSelectedMessageThreadPanel(ConversationPanel panel) {
        jTabbedPane1.setSelectedComponent(panel);
    }
    
    public void requestFocusOnLastInputTextPane() {
        int lastTabIndex = jTabbedPane1.getTabCount() - 1;
        jTabbedPane1.setSelectedIndex(lastTabIndex);
        
        ((ConversationPanel) jTabbedPane1
                .getSelectedComponent())
                .requestFocusOnInputTextPane();
    }
            
}
